package com.example.feign.fallback;

import com.example.dto.StorageDTO;
import com.example.exception.BaseException;
import com.example.exception.ExceptionTool;
import com.example.feign.StorageFeignClient;
import com.example.model.api.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeoutException;


/**
 * 用户接口 FallbackFactory
 *
 * @author Mark sunlightcs@gmail.com
 */
@Slf4j
@Component
public class StorageFeignClientFallbackFactory implements FallbackFactory<StorageFeignClient> {
    @Override
    public StorageFeignClient create(Throwable throwable) {
        return new StorageFeignClient() {

            @Override
            public Result<String> updateStorage(StorageDTO storageDto) {
                try {
                    throw new BaseException(throwable, HttpStatus.SERVICE_UNAVAILABLE);
                } catch (Exception e) {
                    ExceptionTool.throwException("feign调用超时", e);
                }
                return null;
            }
        };
    }
}
